package com.indusfo.spc.service.impl;

import com.indusfo.spc.exception.GlobalException;
import com.indusfo.spc.exception.ModifyFailedException;
import com.indusfo.spc.exception.ParamsErrorException;
import com.indusfo.spc.mapper.*;
import com.indusfo.spc.pojo.*;
import com.indusfo.spc.service.OrderRepairService;
import com.indusfo.spc.vo.HintMessage;
import com.indusfo.spc.vo.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * @Author liub
 * @Date 2019/11/27 9:32
 * @Version 1.0
 */

@Service
public class OrderRepairServiceImpl implements OrderRepairService {

    /**
     * 声明日志的静态变量
     */
    private static final Logger logger = LoggerFactory.getLogger(OrderRepairServiceImpl.class);

    @Resource
    private OrderRepairMapper orderRepairMapper;

    @Resource
    private OrderRepairFlowMapper orderRepairFlowMapper;

    @Resource
    private OrderRepairDeteMapper orderRepairDeteMapper;


    @Resource
    private OrderFlowMapper orderFlowMapper;

    @Resource
    private OrderStationMapper orderStationMapper;

    @Resource
    private OrderDeteMapper orderDeteMapper;

    @Resource
    private OrderBomMapper orderBomMapper;

    @Resource
    private OrderBadphenoMapper orderBadphenoMapper;



    @Resource
    private RepairDetailMapper repairDetailMapper;


    @Resource
    private OrderTechMapper orderTechMapper;

    @Resource
    private OrderMapper orderMapper;

    @Resource
    private BatchMapper batchMapper;


    /**
     * 查询
     */
    @Override
    public JSONObject queryOrderRepair(OrderRepair orderRepair) {
        try {
            if (orderRepair.getOrderTechId() == null) {
                throw new ModifyFailedException(HintMessage.ORDERTECHID_NULL);
            }
            //获取页面的数据个数
            Integer pagesize = orderRepair.getPagesize();
            //获取查询的页数
            Integer pageindex = orderRepair.getPageindex();
            if (pagesize != null && pageindex != null) {
                orderRepair.setIncept(pagesize * (pageindex - 1));
            }
            //执行查询操作
            List<OrderRepair> orderRepairList = orderRepairMapper.queryOrderRepair(orderRepair);

            //如果页数和页面数量都不为空的情况下进行计数
            if (orderRepairList.isEmpty()) {
                return JSONObject.oK(HintMessage.QUERY_NULL, orderRepairList, 0);
            } else {
                Integer count = orderRepairMapper.countOrderRepair(orderRepair);
                return JSONObject.oK(HintMessage.QUERY_SUCCESS, orderRepairList, count);
            }

        } catch (GlobalException e) {
            logger.error(e.getMessage(), e);
            return JSONObject.build(500, e.getMessage());

        }
    }

    /**
     * 新增
     */
    @Override
    public JSONObject insertOrderRepair(OrderRepair orderRepair) {
        try {
            if (orderRepair.getOrderRepairId() != null) {
                throw new ModifyFailedException(HintMessage.INSERT_ID);
            }
            //初始化基础数据
            if (orderRepair.getDataState() == null) {
                orderRepair.setDataState(1);
            }
            //判重
            checkParam(orderRepair);
            //执行新增操作
            Integer row = orderRepairMapper.insertOrderRepair(orderRepair);
            if (row == 0) {
                throw new ModifyFailedException(HintMessage.INSERT_DEFEATED);
            }
            return JSONObject.oK(HintMessage.INSERT_SUCCESS);
        } catch (GlobalException e) {
            logger.error(e.getMessage());
            return JSONObject.build(500, e.getMessage());
        }

    }

    /**
     * 修改
     */
    @Override
    public JSONObject updateOrderRepair(OrderRepair orderRepair) {
        try {
            if (orderRepair.getOrderRepairId() == null) {
                throw new ParamsErrorException(HintMessage.UPDATE_ID);
            }
            //初始化基础数据
            if (orderRepair.getDataState() == null) {
                orderRepair.setDataState(1);
            }
            //判重
            checkParam(orderRepair);
            //执行修改操作
            int row = orderRepairMapper.updateOrderRepair(orderRepair);
            if (row == 0) {
                throw new ModifyFailedException(HintMessage.UPDATE_DEFEATED);
            }
            return JSONObject.oK(HintMessage.UPDATE_SUCCESS);
        } catch (GlobalException e) {//捕获自定义异常
            logger.error(e.getMessage());
            return JSONObject.build(500, e.getMessage());
        }
    }

    /**
     * 删除
     */
    @Override
    public JSONObject deleteOrderRepair(List<Integer> orderRepairIds, Integer dataState) {
        try {
            //空值判断
            if (dataState == null) {
                throw new ParamsErrorException(HintMessage.DATASTATE_NULL);
            }
            if (orderRepairIds == null) {
                throw new ParamsErrorException(HintMessage.LIST_NULL);
            }
            //记录异常信息
            String msg = "";
            switch (dataState) {
                case 1:
                    msg = HintMessage.START_VALUE;
                    break;
                case 2:
                    msg = HintMessage.DELETE_VALUE;
                    break;
                case 3:
                    msg = HintMessage.STOP_VALUE;
                    break;
                default:
                    throw new ParamsErrorException(HintMessage.VIOLATION_OPERATION);
            }
            //执行删除操作
            int row = orderRepairMapper.deleteOrderRepair(orderRepairIds, dataState);
            //声明msg字符串，用来存抛出的异常
            if (row == 0) {
                msg = msg + HintMessage.DEFEATED_VALUE;
            } else {
                msg = msg + HintMessage.SUCCESS_VALUE;
            }
            return JSONObject.oK(msg);
        } catch (GlobalException e) {
            logger.error(e.getMessage());
            return JSONObject.build(500, e.getMessage());
        }
    }

    /**
     * 级联删除维修流程关联的工序、维修采集参数
     */
    @Transactional
    @Override
    public JSONObject deleteCascadeMatRepair(List<Integer> orderRepairIds, Integer dataState) {
        //空值判断
        if (dataState == null) {
            throw new ParamsErrorException(HintMessage.DATASTATE_NULL);
        }
        if (orderRepairIds == null) {
            throw new ParamsErrorException(HintMessage.LIST_NULL);
        }
        //记录异常信息
        String msg = "";
        switch (dataState) {
            case 1:
                msg = HintMessage.START_VALUE;
                break;
            case 2:
                msg = HintMessage.DELETE_VALUE;
                break;
            case 3:
                msg = HintMessage.STOP_VALUE;
                break;
            default:
                throw new ParamsErrorException(HintMessage.VIOLATION_OPERATION);
        }
        //执行删除操作
        int row = orderRepairMapper.deleteOrderRepair(orderRepairIds, dataState);

        //根据传入的orderRepairIds查询对应的工序和维修采集参数
        for (Integer orderRepairId : orderRepairIds) {

            //级联执行工序删除
            List<OrderRepairFlow> orderRepairFlowList = orderRepairFlowMapper.queryOrderRepairFlowByOrderRepairId(orderRepairId);
            if (orderRepairFlowList != null && !orderRepairFlowList.isEmpty()) {
                for (OrderRepairFlow orderRepairFlow : orderRepairFlowList) {
                    orderRepairFlowMapper.deleteOrderRepairFlowByOrderRepairFlowId(orderRepairFlow.getOrderRepairFlowId(), dataState);
                }
            }

            //级联执行维修采集项删除
            List<OrderRepairDete> orderRepairDeteList = orderRepairDeteMapper.queryOrderRepairDeteByOrderRepairId(orderRepairId);
            if (orderRepairDeteList != null && !orderRepairDeteList.isEmpty()) {
                for (OrderRepairDete orderRepairDete : orderRepairDeteList) {
                    orderRepairDeteMapper.deleteOrderRepairDeteByOrderRepairDeteId(orderRepairDete.getOrderRepairDeteId(), dataState);
                }
            }
        }

        //声明msg字符串，用来存抛出的异常
        if (row == 0) {
            msg = msg + HintMessage.DEFEATED_VALUE;
        } else {
            msg = msg + HintMessage.SUCCESS_VALUE;
        }
        return JSONObject.oK(msg);
    }
    /**
     * @Author 熊冰 保存维修流程 工序 复制
     * @Date 2019/12/30 13:47
     */
    @Override
    public JSONObject copyOrderRepairFlow(OrderRepair orderRepair20) {
        try {
            //判空
            if (orderRepair20 == null) {
                throw new ParamsErrorException(HintMessage.DATA_NULL);
            }
            if (orderRepair20.getBatchId() == null) {
                throw new ParamsErrorException(HintMessage.BATCHID_NULL);
            }
            Integer batchId=orderRepair20.getBatchId();
            if (orderRepair20.getOrderTechId()==null){
                throw new ParamsErrorException(HintMessage.ORDERTECHID_NULL);
            }
            Integer oldOrderTechId=orderRepair20.getOrderTechId();

            JSONObject jsonObject=new JSONObject();



            if(orderRepair20.getBatchId().equals(0)) {
                //任务单保存
                int row= orderMapper.queryOrderTechCount(oldOrderTechId);
                if(row!=0){
                //复制新流程
                //查询旧版本
                //复制版本
                Integer newOrderTechId = copyTech(oldOrderTechId,1);
                //复制流程完成
                //获取前端流程
                List<OrderRepairFlow> orderRepairFlowList9 = orderRepair20.getOrderRepairFlowList();
                //查询新本本下的工位
                //查询旧流程
                OrderRepair orderRepair21 = new OrderRepair();
                orderRepair21.setOrderTechId(oldOrderTechId);
                orderRepair21.setOrderRepairId(orderRepair20.getOrderRepairId());
                List<OrderRepair> orderRepairList21 = orderRepairMapper.queryOrderRepair(orderRepair21);
                if (orderRepairList21.size() != 0) {
                    orderRepair21 = orderRepairList21.get(0);
                }
                //查询新流程
                OrderRepair orderRepair22 = new OrderRepair();
                orderRepair22.setOrderTechId(newOrderTechId);
                List<OrderRepair> orderRepairList22 = orderRepairMapper.queryOrderRepair(orderRepair22);
                for (OrderRepair orderRepair : orderRepairList22) {
                    if (orderRepair.getRepairId().equals(orderRepair21.getRepairId())) {
                        BeanUtils.copyProperties(orderRepair21, orderRepair22);
                        orderRepair22.setOrderRepairId(orderRepair.getOrderRepairId());

                    }

                }

                Integer newOrderRepairId = orderRepair22.getOrderRepairId();

                //查询新流程
                OrderRepairFlow orderRepairFlow8 = new OrderRepairFlow();
                orderRepairFlow8.setOrderRepairId(newOrderRepairId);
                List<OrderRepairFlow> orderRepairFlowList8 = orderRepairFlowMapper.queryOrderRepairFlow(orderRepairFlow8);
                //查询旧流程
                OrderRepairFlow orderRepairFlow7 = new OrderRepairFlow();
                orderRepairFlow7.setOrderRepairId(orderRepair20.getOrderRepairId());
                List<OrderRepairFlow> orderRepairFlowList7 = orderRepairFlowMapper.queryOrderRepairFlow(orderRepairFlow7);
                    //orderRepairFlowList9 旧流
                    // orderRepairFlowList7 新流程
                if (orderRepairFlowList7.size() != 0 && orderRepairFlowList8.size() != 0) {
                    for (OrderRepairFlow orderRepairFlow6 : orderRepairFlowList9) {
                        if (!orderRepairFlow6.getOrderRepairFlowId().equals(0)) {
                            for (int i = 0; i < orderRepairFlowList7.size(); i++) {
                                if (orderRepairFlowList7.get(i).getOrderRepairFlowId().equals(orderRepairFlow6.getOrderRepairFlowId())) {
                                    orderRepairFlow6.setOrderRepairFlowId(orderRepairFlowList8.get(i).getOrderRepairFlowId());

                                }
                                if (orderRepairFlowList7.get(i).getOrderFlowId().equals(orderRepairFlow6.getOrderFlowId())) {
                                    orderRepairFlow6.setOrderFlowId(orderRepairFlowList8.get(i).getOrderFlowId());
                                }
                            }
                        }
                    }
                }
                OrderFlow newOrderFlow=new OrderFlow();
                newOrderFlow.setOrderTechId(newOrderTechId);
                OrderFlow oldOrderFlow=new OrderFlow();
                oldOrderFlow.setOrderTechId(oldOrderTechId);
                List<OrderFlow> orderFlowList12=orderFlowMapper.queryOrderFlow(newOrderFlow);
                List<OrderFlow> orderFlowList13=orderFlowMapper.queryOrderFlow(oldOrderFlow);

                    for (OrderRepairFlow orderRepairFlow11:orderRepairFlowList9  ) {
                        for (int i = 0; i < orderFlowList13.size(); i++) {
                            if(orderRepairFlow11.getOrderFlowId().equals(orderFlowList13.get(i).getOrderFlowId())){
                                orderRepairFlow11.setOrderFlowId(orderFlowList12.get(i).getOrderFlowId());
                            }
                        }
                    }


                //复制新对象

                OrderRepair newOrderRepair = new OrderRepair();
                BeanUtils.copyProperties(orderRepair20, newOrderRepair);
                newOrderRepair.setOrderTechId(newOrderTechId);
                newOrderRepair.setOrderRepairId(newOrderRepairId);
                newOrderRepair.setOrderRepairFlowList(null);
                newOrderRepair.setOrderRepairFlowList(orderRepairFlowList9);
                //修改新流程
                jsonObject = saveOrderRepairFlow(newOrderRepair);
            }else {
                jsonObject = saveOrderRepairFlow(orderRepair20);

            }



            }else {
                //批次保存
                OrderTech orderTech10 = new OrderTech();
                orderTech10.setOrderTechId(orderRepair20.getOrderTechId());
                List<OrderTech> orderTeches10 = orderTechMapper.queryOrderTech(orderTech10);
                if (orderTeches10.size() != 0) {
                    orderTech10 = orderTeches10.get(0);
                }
                System.out.println(orderTech10.getOrderTechId() + "-" + orderTech10.getSourceTypeId());
                if (orderTech10.getSourceTypeId().equals(2)) {
                    jsonObject = saveOrderRepairFlow(orderRepair20);

                } else {
                    //任务单保存

                    //复制新流程
                    //查询旧版本

                    //复制版本
                    Integer newOrderTechId=copyTech(oldOrderTechId,2);
                    //复制流程完成
                    //获取前端流程
                    List<OrderRepairFlow> orderRepairFlowList9 = orderRepair20.getOrderRepairFlowList();
                    //查询新本本下的工位
                    //查询旧流程
                    OrderRepair orderRepair21=new OrderRepair();
                    orderRepair21.setOrderTechId(oldOrderTechId);
                    orderRepair21.setOrderRepairId(orderRepair20.getOrderRepairId());
                    List<OrderRepair> orderRepairList21 = orderRepairMapper.queryOrderRepair(orderRepair21);
                    if(orderRepairList21.size()!=0){
                        orderRepair21=orderRepairList21.get(0);
                    }
                    //查询新流程
                    OrderRepair orderRepair22=new OrderRepair();
                    orderRepair22.setOrderTechId(newOrderTechId);
                    List<OrderRepair> orderRepairList22 = orderRepairMapper.queryOrderRepair(orderRepair22);
                    for (OrderRepair orderRepair:orderRepairList22  ) {
                        if(orderRepair.getRepairId().equals(orderRepair21.getRepairId())){
                            BeanUtils.copyProperties(orderRepair21,orderRepair22);
                            orderRepair22.setOrderRepairId(orderRepair.getOrderRepairId());
                        }

                    }

                    Integer newOrderRepairId=orderRepair22.getOrderRepairId();

                    //查询新流程
                    OrderRepairFlow orderRepairFlow8=new OrderRepairFlow();
                    orderRepairFlow8.setOrderRepairId(newOrderRepairId);
                    List<OrderRepairFlow> orderRepairFlowList8 = orderRepairFlowMapper.queryOrderRepairFlow(orderRepairFlow8);
                    //查询旧流程
                    OrderRepairFlow orderRepairFlow7=new OrderRepairFlow();
                    orderRepairFlow7.setOrderRepairId(orderRepair20.getOrderRepairId());
                    List<OrderRepairFlow> orderRepairFlowList7 = orderRepairFlowMapper.queryOrderRepairFlow(orderRepairFlow7);
                    if(orderRepairFlowList7.size()!=0&&orderRepairFlowList8.size()!=0) {
                        for (OrderRepairFlow orderRepairFlow6 : orderRepairFlowList9) {
                            if (!orderRepairFlow6.getOrderRepairFlowId().equals(0)) {
                                for (int i = 0; i < orderRepairFlowList7.size(); i++) {
                                    if (orderRepairFlowList7.get(i).getOrderRepairFlowId().equals(orderRepairFlow6.getOrderRepairFlowId())) {
                                        orderRepairFlow6.setOrderRepairFlowId(orderRepairFlowList8.get(i).getOrderRepairFlowId());
                                    }
                                    if(orderRepairFlowList7.get(i).getOrderFlowId().equals(orderRepairFlow6.getOrderFlowId())){
                                        orderRepairFlow6.setOrderFlowId(orderRepairFlowList8.get(i).getOrderFlowId());
                                    }
                                }
                            }
                        }
                    }
                    //复制新对象

                    OrderFlow newOrderFlow=new OrderFlow();
                    newOrderFlow.setOrderTechId(newOrderTechId);
                    OrderFlow oldOrderFlow=new OrderFlow();
                    oldOrderFlow.setOrderTechId(oldOrderTechId);
                    List<OrderFlow> orderFlowList12=orderFlowMapper.queryOrderFlow(newOrderFlow);
                    List<OrderFlow> orderFlowList13=orderFlowMapper.queryOrderFlow(oldOrderFlow);

                    for (OrderRepairFlow orderRepairFlow11:orderRepairFlowList9  ) {
                        for (int i = 0; i < orderFlowList13.size(); i++) {
                            if(orderRepairFlow11.getOrderFlowId().equals(orderFlowList13.get(i).getOrderFlowId())){
                                orderRepairFlow11.setOrderFlowId(orderFlowList12.get(i).getOrderFlowId());
                            }
                        }
                    }


                    OrderRepair   newOrderRepair=new OrderRepair();
                    BeanUtils.copyProperties(orderRepair20,newOrderRepair);
                    newOrderRepair.setOrderTechId(newOrderTechId);
                    newOrderRepair.setOrderRepairId(newOrderRepairId);
                    newOrderRepair.setOrderRepairFlowList(null);
                    newOrderRepair.setOrderRepairFlowList(orderRepairFlowList9);
                    //修改新流程
                    jsonObject=saveOrderRepairFlow(newOrderRepair);
                    Batch batch=new Batch();
                    batch.setBatchId(orderRepair20.getBatchId());
                    batch.setOrderTechId(newOrderTechId);
                    batchMapper.updateBatch(batch);
                }
                //更新批次流程的当前工序
                Batch batch=new Batch();
                batch.setBatchId(orderRepair20.getBatchId());
                Integer nowFlowId = orderTechMapper.selectNowFlowId(orderRepair20.getBatchId());
                if(nowFlowId==null){
                    nowFlowId=orderTechMapper.selectLastFlowId(orderRepair20.getBatchId());
                }
                batch.setOrderFlowId(nowFlowId);
                batchMapper.updateBatch(batch);
            }


            return jsonObject;
        } catch (GlobalException e) {// 捕获自定义异常
            logger.error(e.getMessage());
            return JSONObject.build(500, e.getMessage());
        }
    }

    /**
     * @Author 熊冰  保存 维修流程 采集项目 复制
     * @Date 2019/12/30 13:48
     */
    @Override
    public JSONObject copyOrderRepairDete(OrderRepair orderRepair20) {
        try {
            //判空
            if (orderRepair20 == null) {
                throw new ParamsErrorException(HintMessage.DATA_NULL);
            }
            if (orderRepair20.getBatchId() == null) {
                throw new ParamsErrorException(HintMessage.BATCHID_NULL);
            }
            Integer batchId=orderRepair20.getBatchId();
            if (orderRepair20.getOrderTechId()==null){
                throw new ParamsErrorException(HintMessage.ORDERTECHID_NULL);
            }
            Integer oldOrderTechId=orderRepair20.getOrderTechId();

            JSONObject jsonObject=new JSONObject();



            if(orderRepair20.getBatchId().equals(0)){
                //任务单保存

                //复制新流程
                //查询旧版本
                int row= orderMapper.queryOrderTechCount(oldOrderTechId);
                if(row!=0) {
                    //复制版本

                    Integer newOrderTechId = copyTech(oldOrderTechId, 1);
                    //复制流程完成
                    //获取前端流程
                    List<OrderRepairDete> orderRepairDeteList9 = orderRepair20.getOrderRepairDeteList();
                    //查询新本本下的工位
                    //查询旧流程
                    OrderRepair orderRepair21 = new OrderRepair();
                    orderRepair21.setOrderTechId(oldOrderTechId);
                    orderRepair21.setOrderRepairId(orderRepair20.getOrderRepairId());
                    List<OrderRepair> orderRepairList21 = orderRepairMapper.queryOrderRepair(orderRepair21);
                    if (orderRepairList21.size() != 0) {
                        orderRepair21 = orderRepairList21.get(0);
                    }
                    //查询新流程
                    OrderRepair orderRepair22 = new OrderRepair();
                    orderRepair22.setOrderTechId(newOrderTechId);
                    List<OrderRepair> orderRepairList22 = orderRepairMapper.queryOrderRepair(orderRepair22);
                    for (OrderRepair orderRepair : orderRepairList22) {
                        if (orderRepair.getRepairId().equals(orderRepair21.getRepairId())) {
                            BeanUtils.copyProperties(orderRepair21, orderRepair22);
                            orderRepair22.setOrderRepairId(orderRepair.getOrderRepairId());
                        }

                    }

                    Integer newOrderRepairId = orderRepair22.getOrderRepairId();

                    //查询新流程
                    OrderRepairDete orderRepairDete8 = new OrderRepairDete();
                    orderRepairDete8.setOrderRepairId(newOrderRepairId);
                    orderRepairDete8.setRepairDetailId(orderRepair20.getRepairDetailId());
                    List<OrderRepairDete> orderRepairDeteList8 = orderRepairDeteMapper.queryOrderRepairDete(orderRepairDete8);
                    //查询旧流程
                    OrderRepairDete orderRepairDete7 = new OrderRepairDete();
                    orderRepairDete7.setOrderRepairId(orderRepair20.getOrderRepairId());
                    orderRepairDete7.setOrderRepairId(orderRepair20.getRepairDetailId());
                    List<OrderRepairDete> orderRepairDeteList7 = orderRepairDeteMapper.queryOrderRepairDete(orderRepairDete7);
                    if (orderRepairDeteList7.size() != 0) {
                        for (OrderRepairDete orderRepairDete6 : orderRepairDeteList9) {
                            if (!orderRepairDete6.getOrderRepairDeteId().equals(0)) {
                                for (int i = 0; i < orderRepairDeteList7.size(); i++) {
                                    if (orderRepairDeteList7.get(i).getOrderRepairDeteId().equals(orderRepairDete6.getOrderRepairDeteId())) {
                                        orderRepairDete6.setOrderRepairDeteId(orderRepairDeteList8.get(i).getOrderRepairDeteId());
                                    }
                                }
                            }
                        }
                    }
                    //复制新对象

                    OrderRepair newOrderRepair = new OrderRepair();
                    BeanUtils.copyProperties(orderRepair20, newOrderRepair);
                    newOrderRepair.setOrderTechId(newOrderTechId);
                    newOrderRepair.setOrderRepairId(newOrderRepairId);
                    newOrderRepair.setOrderRepairDeteList(null);
                    newOrderRepair.setOrderRepairDeteList(orderRepairDeteList9);
                    //修改新流程
                    jsonObject = saveOrderRepairDete(newOrderRepair);

                }else{
                    jsonObject = saveOrderRepairDete(orderRepair20);
                }






            }else {
                //批次保存
                OrderTech orderTech10 = new OrderTech();
                orderTech10.setOrderTechId(orderRepair20.getOrderTechId());
                List<OrderTech> orderTeches10 = orderTechMapper.queryOrderTech(orderTech10);
                if (orderTeches10.size() != 0) {
                    orderTech10 = orderTeches10.get(0);
                }
                System.out.println(orderTech10.getOrderTechId() + "-" + orderTech10.getSourceTypeId());
                if (orderTech10.getSourceTypeId().equals(2)) {
                    jsonObject = saveOrderRepairDete(orderRepair20);

                } else {
                    //任务单保存

                    //复制新流程
                    //复制版本
                    Integer newOrderTechId=copyTech(oldOrderTechId,2);
                    //复制流程完成
                    //获取前端流程
                    List<OrderRepairDete> orderRepairDeteList9 = orderRepair20.getOrderRepairDeteList();
                    //查询新本本下的工位
                    //查询旧流程
                    OrderRepair orderRepair21=new OrderRepair();
                    orderRepair21.setOrderTechId(oldOrderTechId);
                    orderRepair21.setOrderRepairId(orderRepair20.getOrderRepairId());
                    List<OrderRepair> orderRepairList21 = orderRepairMapper.queryOrderRepair(orderRepair21);
                    if(orderRepairList21.size()!=0){
                        orderRepair21=orderRepairList21.get(0);
                    }
                    //查询新流程
                    OrderRepair orderRepair22=new OrderRepair();
                    orderRepair22.setOrderTechId(newOrderTechId);
                    List<OrderRepair> orderRepairList22 = orderRepairMapper.queryOrderRepair(orderRepair22);
                    for (OrderRepair orderRepair:orderRepairList22  ) {
                        if(orderRepair.getRepairId().equals(orderRepair21.getRepairId())){
                            BeanUtils.copyProperties(orderRepair21,orderRepair22);
                            orderRepair22.setOrderRepairId(orderRepair.getOrderRepairId());
                        }

                    }

                    Integer newOrderRepairId=orderRepair22.getOrderRepairId();

                    //查询新流程
                    OrderRepairDete orderRepairDete8=new OrderRepairDete();
                    orderRepairDete8.setOrderRepairId(newOrderRepairId);
                    orderRepairDete8.setRepairDetailId(orderRepair20.getRepairDetailId());
                    List<OrderRepairDete> orderRepairDeteList8 = orderRepairDeteMapper.queryOrderRepairDete(orderRepairDete8);
                    //查询旧流程
                    OrderRepairDete orderRepairDete7=new OrderRepairDete();
                    orderRepairDete7.setOrderRepairId(orderRepair20.getOrderRepairId());
                    orderRepairDete7.setRepairDetailId(orderRepair20.getRepairDetailId());
                    List<OrderRepairDete> orderRepairDeteList7 = orderRepairDeteMapper.queryOrderRepairDete(orderRepairDete7);
                    if(orderRepairDeteList7.size()!=0) {
                        for (OrderRepairDete orderRepairDete6 : orderRepairDeteList9) {
                            if (!orderRepairDete6.getOrderRepairDeteId().equals(0)) {
                                for (int i = 0; i < orderRepairDeteList7.size(); i++) {
                                    if (orderRepairDeteList7.get(i).getOrderRepairDeteId().equals(orderRepairDete6.getOrderRepairDeteId())) {
                                        orderRepairDete6.setOrderRepairDeteId(orderRepairDeteList8.get(i).getOrderRepairDeteId());
                                    }
                                }
                            }
                        }
                    }
                    //复制新对象

                    OrderRepair   newOrderRepair=new OrderRepair();
                    BeanUtils.copyProperties(orderRepair20,newOrderRepair);
                    newOrderRepair.setOrderTechId(newOrderTechId);
                    newOrderRepair.setOrderRepairId(newOrderRepairId);
                    newOrderRepair.setOrderRepairDeteList(null);
                    newOrderRepair.setOrderRepairDeteList(orderRepairDeteList9);
                    //修改新流程
                    jsonObject=saveOrderRepairDete(newOrderRepair);

                    Batch batch=new Batch();
                    batch.setBatchId(orderRepair20.getBatchId());
                    batch.setOrderTechId(newOrderTechId);
                    batchMapper.updateBatch(batch);

                }
                //更新批次流程的当前工序
                Batch batch=new Batch();
                batch.setBatchId(orderRepair20.getBatchId());
                Integer nowFlowId = orderTechMapper.selectNowFlowId(orderRepair20.getBatchId());
                if(nowFlowId==null){
                    nowFlowId=orderTechMapper.selectLastFlowId(orderRepair20.getBatchId());
                }
                batch.setOrderFlowId(nowFlowId);
                batchMapper.updateBatch(batch);
            }


            return jsonObject;
        } catch (GlobalException e) {// 捕获自定义异常
            logger.error(e.getMessage());
            return JSONObject.build(500, e.getMessage());
        }
    }

    /**
     * 判重
     */
    public void checkParam(OrderRepair orderRepair) {
        //维修ID
        Integer repairId = orderRepair.getRepairId();
        //任务单版本ID
        Integer orderTechId = orderRepair.getOrderTechId();
        //维修ID
        if (repairId == null) {
            throw new ParamsErrorException(HintMessage.REPAIRID_NULL);
        }
        //任务单版本ID
        if (orderTechId == null) {
            throw new ParamsErrorException(HintMessage.ORDERTECHID_NULL);
        }

        // 执行判重操作
        Integer Counts = orderRepairMapper.checkOrderRepair(orderRepair);
        if (Counts != 0) {
            throw new ParamsErrorException(HintMessage.REPEATING_DATA);
        }

    }

    /**
     * 任务单流程维修工序
     */
    @Override
    public JSONObject saveOrderRepairFlow(OrderRepair orderRepair) {
        try {
            //判空
            if (orderRepair == null) {
                throw new ParamsErrorException(HintMessage.DATA_NULL);
            }
            if (orderRepair.getOrderRepairId() == null) {
                throw new ParamsErrorException(HintMessage.ORDERFLOWID_NULL);
            }
            //获取流程id
            Integer orderRepairId = orderRepair.getOrderRepairId();
            //获取list集合
            List<OrderRepairFlow> orderRepairFlowList = new ArrayList<>();
            orderRepairFlowList = orderRepair.getOrderRepairFlowList();
            for (OrderRepairFlow orderRepairFlow : orderRepairFlowList) {
                orderRepairFlow.setOrderRepairId(orderRepairId);
            }


            //获取数据库数据
            OrderRepairFlow orderRepairFlow1 = new OrderRepairFlow();
            orderRepairFlow1.setOrderRepairId(orderRepairId);
            List<OrderRepairFlow> oldorderRepairFlowList = orderRepairFlowMapper.queryOrderRepairFlow(orderRepairFlow1);
            //删除
            List<Integer> deleteList = new ArrayList<>();
            List<Integer> oldIds = new ArrayList<>();
            List<Integer> newIds = new ArrayList<>();
            if (oldorderRepairFlowList != null) {
                for (OrderRepairFlow orderRepairFlow : oldorderRepairFlowList
                ) {
                    oldIds.add(orderRepairFlow.getOrderRepairFlowId());
                }
            }
            if (orderRepairFlowList != null) {
                for (OrderRepairFlow orderRepairFlow : orderRepairFlowList
                ) {
                    newIds.add(orderRepairFlow.getOrderRepairFlowId());
                }
            }
            HashSet h1 = new HashSet(oldIds);
            HashSet h2 = new HashSet(newIds);
            h1.removeAll(h2);
            deleteList.addAll(h1);
            int deleterow = 0;
            int insertrow = 0;
            int updaterow = 0;
            if (deleteList.size() != 0) {
                deleterow = orderRepairFlowMapper.deleteOrderRepairFlow(deleteList, 2);
            }
            //删除完成  进行新增 和 修改
            if (orderRepairFlowList != null) {
                for (OrderRepairFlow orderRepairFlow : orderRepairFlowList
                ) {
                    if (orderRepairFlow.getDataState() == null) {
                        orderRepairFlow.setDataState(1);
                    }


                    if (orderRepairFlow.getOrderRepairFlowId() != null) {
                        if (orderRepairFlow.getOrderRepairFlowId().equals(0)) {
                            orderRepairFlow.setOrderRepairFlowId(null);
                            // 执行判重操作
                            Integer Counts = orderRepairFlowMapper.checkOrderRepairFlow(orderRepairFlow);
                            if (Counts == 0) {
                                insertrow = insertrow + orderRepairFlowMapper.insertOrderRepairFlow(orderRepairFlow);
                            }

                        } else {
                            // 执行判重操作
                            Integer Counts = orderRepairFlowMapper.checkOrderRepairFlow(orderRepairFlow);
                            if (Counts == 0) {
                                updaterow = updaterow + orderRepairFlowMapper.updateOrderRepairFlow(orderRepairFlow);
                            }
                        }
                    }
                }
            }
            return JSONObject.oK(HintMessage.SAVE_SUCCESS, orderRepair,deleterow + insertrow + updaterow);
        } catch (GlobalException e) {// 捕获自定义异常
            logger.error(e.getMessage());
            return JSONObject.build(500, e.getMessage());
        }
    }

    /**
     * 任务单流程维修采集参数
     */
    @Override
    public JSONObject saveOrderRepairDete(OrderRepair orderRepair) {
        try {
            //判空
            if (orderRepair == null) {
                throw new ParamsErrorException(HintMessage.DATA_NULL);
            }
            if (orderRepair.getOrderRepairId() == null) {
                throw new ParamsErrorException(HintMessage.ORDERREPAIRID_NULL);
            }
            if (orderRepair.getRepairDetailId() == null) {
                throw new ParamsErrorException(HintMessage.REPAIRDETAILID_NULL);
            }


            //获取流程id
            Integer orderRepairId = orderRepair.getOrderRepairId();
            Integer repairDetailId = orderRepair.getRepairDetailId();
            //获取list集合
            List<OrderRepairDete> orderRepairDeteList = new ArrayList<>();
            orderRepairDeteList = orderRepair.getOrderRepairDeteList();
            for (OrderRepairDete orderRepairDete : orderRepairDeteList) {
                orderRepairDete.setOrderRepairId(orderRepairId);
                orderRepairDete.setRepairDetailId(repairDetailId);
            }


            //获取数据库数据
            OrderRepairDete orderRepairDete1 = new OrderRepairDete();
            orderRepairDete1.setOrderRepairId(orderRepairId);
            orderRepairDete1.setRepairDetailId(repairDetailId);
            List<OrderRepairDete> oldorderRepairDeteList = orderRepairDeteMapper.queryOrderRepairDete(orderRepairDete1);
            //删除
            List<Integer> deleteList = new ArrayList<>();
            List<Integer> oldIds = new ArrayList<>();
            List<Integer> newIds = new ArrayList<>();
            if (oldorderRepairDeteList != null) {
                for (OrderRepairDete orderRepairDete : oldorderRepairDeteList
                ) {
                    oldIds.add(orderRepairDete.getOrderRepairDeteId());
                }
            }
            if (orderRepairDeteList != null) {
                for (OrderRepairDete orderRepairDete : orderRepairDeteList
                ) {
                    newIds.add(orderRepairDete.getOrderRepairDeteId());
                }
            }
            HashSet h1 = new HashSet(oldIds);
            HashSet h2 = new HashSet(newIds);
            h1.removeAll(h2);
            deleteList.addAll(h1);
            int deleterow = 0;
            int insertrow = 0;
            int updaterow = 0;
            if (deleteList.size() != 0) {
                deleterow = orderRepairDeteMapper.deleteOrderRepairDete(deleteList, 2);
            }
            //删除完成  进行新增 和 修改
            if (orderRepairDeteList != null) {
                for (OrderRepairDete orderRepairDete : orderRepairDeteList
                ) {
                    if (orderRepairDete.getDataState() == null) {
                        orderRepairDete.setDataState(1);
                    }


                    if (orderRepairDete.getOrderRepairDeteId() != null) {
                        if (orderRepairDete.getOrderRepairDeteId().equals(0)) {
                            orderRepairDete.setOrderRepairDeteId(null);
                            // 执行判重操作
                            Integer Counts = orderRepairDeteMapper.checkOrderRepairDete(orderRepairDete);
                            if (Counts == 0) {
                                insertrow = insertrow + orderRepairDeteMapper.insertOrderRepairDete(orderRepairDete);
                            }

                        } else {
                            // 执行判重操作
                            Integer Counts = orderRepairDeteMapper.checkOrderRepairDete(orderRepairDete);
                            if (Counts == 0) {
                                updaterow = updaterow + orderRepairDeteMapper.updateOrderRepairDete(orderRepairDete);
                            }
                        }
                    }
                }
            }
            return JSONObject.oK(HintMessage.SAVE_SUCCESS, orderRepair,deleterow + insertrow + updaterow);
        } catch (GlobalException e) {// 捕获自定义异常
            logger.error(e.getMessage());
            return JSONObject.build(500, e.getMessage());
        }
    }
    public Integer copyTech(Integer oldOrderTechId,Integer sourceTypeId ){
        //复制新流程
        //查询旧版本


        OrderTech oldOrderTech=new OrderTech();
        oldOrderTech.setOrderTechId(oldOrderTechId);
        //查询原版本
        List<OrderTech> orderTeches = orderTechMapper.queryOrderTech(oldOrderTech);
        if(orderTeches.size()!=0){
            oldOrderTech=orderTeches.get(0);
        }
        //创建新版本

        OrderTech newOrderTech=new OrderTech();
        BeanUtils.copyProperties(oldOrderTech,newOrderTech);
        newOrderTech.setOrderTechId(null);
        newOrderTech.setSourceTypeId(sourceTypeId);


        orderTechMapper.insertOrderTech(newOrderTech);
        Integer  newOrderTechId=newOrderTech.getOrderTechId();
        //新增完成

        //复制流程
        OrderFlow orderFlow1=new OrderFlow();
        orderFlow1.setOrderTechId(oldOrderTechId);
        List<OrderFlow> orderFlowList = orderFlowMapper.queryOrderFlow(orderFlow1);

        List<OrderStation> orderStationInsertList=new ArrayList<>();
        List<OrderDete> orderDeteInsertList=new ArrayList<>();
        List<OrderBom> orderBomInsertList=new ArrayList<>();
        List<OrderBadpheno> orderBadphenoInsertList=new ArrayList<>();
        List<OrderRepairFlow> orderRepairFlowInsertList=new ArrayList<>();
        List<OrderRepairDete> orderRepairDeteInsertList=new ArrayList<>();

        for (OrderFlow oldOrderFlow:orderFlowList ) {

            OrderFlow orderFlowInsert=new OrderFlow();

            Integer oldOrderFlowId=oldOrderFlow.getOrderFlowId();

            OrderFlow newOrderFlow=new OrderFlow();
            BeanUtils.copyProperties(oldOrderFlow,newOrderFlow);
            newOrderFlow.setOrderTechId(newOrderTechId);
            newOrderFlow.setOrderFlowId(null);
            //新增流程
            orderFlowMapper.insertOrderFlow(newOrderFlow);
            Integer newOrderFlowId=newOrderFlow.getOrderFlowId();

            //复制工位

            List<OrderStation> orderStations = orderStationMapper.queryOrderStationByOrderFlowId(oldOrderFlowId);
            for (OrderStation orderStation:orderStations ) {
                orderStation.setOrderFlowId(newOrderFlowId);
                orderStation.setOrderStationId(null);
                orderStationInsertList.add(orderStation);
//                orderStationMapper.insertOrderStation(orderStation);
            }

            List<OrderDete> orderDetes = orderDeteMapper.queryOrderDeteByOrderFlowId(oldOrderFlowId);
            for (OrderDete orderDete:orderDetes ) {
                orderDete.setOrderFlowId(newOrderFlowId);
                orderDete.setOrderDeteId(null);
                orderDeteInsertList.add(orderDete);
//                orderDeteMapper.insertOrderDete(orderDete);
            }

            List<OrderBom> orderBoms = orderBomMapper.queryOrderBomByOrderFlowId(oldOrderFlowId);
            for (OrderBom orderBom:orderBoms ) {
                orderBom.setOrderFlowId(newOrderFlowId);
                orderBom.setOrderBomId(null);
                orderBomInsertList.add(orderBom);
//                orderBomMapper.insertOrderBom(orderBom);
            }

            List<OrderBadpheno> orderBadphenos = orderBadphenoMapper.queryOrderBadphenoByOrderFlowId(oldOrderFlowId);
            for (OrderBadpheno orderBadpheno:orderBadphenos ) {
                orderBadpheno.setOrderFlowId(newOrderFlowId);
                orderBadpheno.setOrderBadphenoId(null);
                orderBadphenoInsertList.add(orderBadpheno);
//                orderBadphenoMapper.insertOrderBadpheno(orderBadpheno);
            }

        }

        OrderRepair orderRepair1=new OrderRepair();
        orderRepair1.setOrderTechId(oldOrderTechId);
        List<OrderRepair> orderRepairList = orderRepairMapper.queryOrderRepair(orderRepair1);

        for (OrderRepair oldOrderRepair:orderRepairList ) {
            Integer oldOrderRepairId= oldOrderRepair.getOrderRepairId();
            OrderRepair newOrderRepair=new OrderRepair();
            BeanUtils.copyProperties(oldOrderRepair,newOrderRepair);
            newOrderRepair.setOrderRepairId(null);
            newOrderRepair.setOrderTechId(newOrderTechId);
            orderRepairMapper.insertOrderRepair(newOrderRepair);
            Integer newOrderRepairId=newOrderRepair.getOrderRepairId();

            List<OrderRepairFlow> orderRepairFlows = orderRepairFlowMapper.queryOrderRepairFlowByOrderRepairId(oldOrderRepairId);
            for (OrderRepairFlow oldOrderRepairFlow:orderRepairFlows ) {

                //查询工艺流程
                OrderFlow orderFlow = new OrderFlow();
                orderFlow.setOrderTechId(newOrderTechId);
                List<OrderFlow> orderFlowList1 = orderFlowMapper.queryOrderFlow(orderFlow);
                for (OrderFlow orderFlow2:orderFlowList1) {
                    if(oldOrderRepairFlow.getProId()!=null&&orderFlow2.getProId()!=null){
                        if(oldOrderRepairFlow.getProId().equals(orderFlow2.getProId())&&oldOrderRepairFlow.getSerialNum().equals(orderFlow2.getSerialNum())){
                            OrderRepairFlow newOrderRepairFlow=new OrderRepairFlow();
                            newOrderRepairFlow.setDataState(1);
                            newOrderRepairFlow.setOrderRepairFlowId(null);
                            newOrderRepairFlow.setOrderRepairId(newOrderRepairId);
                            newOrderRepairFlow.setOrderFlowId(orderFlow2.getOrderFlowId());
                            orderRepairFlowInsertList.add(newOrderRepairFlow);
//                            orderRepairFlowMapper.insertOrderRepairFlow(newOrderRepairFlow);


                        }
                    }



                }


            }

            RepairDetail repairDetail=new RepairDetail();
            repairDetail.setRepairId(oldOrderRepair.getRepairId());
            List<RepairDetail> repairDetailList = repairDetailMapper.selectAll(repairDetail);
            for (RepairDetail repairDetail1 : repairDetailList) {
                OrderRepairDete orderRepairDete1=new OrderRepairDete();
                orderRepairDete1.setRepairDetailId(repairDetail1.getRepairDetailId());
                orderRepairDete1.setOrderRepairId(oldOrderRepair.getOrderRepairId());
                List<OrderRepairDete> orderRepairDeteList = orderRepairDeteMapper.queryOrderRepairDete(orderRepairDete1);


                for (OrderRepairDete oldOrderRepairDete : orderRepairDeteList) {

                    OrderRepairDete newOrderRepairDete = new OrderRepairDete();
                    BeanUtils.copyProperties(oldOrderRepairDete,newOrderRepairDete);
                    newOrderRepairDete.setOrderRepairDeteId(null);
                    newOrderRepairDete.setOrderRepairId(newOrderRepairId);
                    orderRepairDeteInsertList.add(newOrderRepairDete);
//                    orderRepairDeteMapper.insertOrderRepairDete(newOrderRepairDete);

                }

            }


        }


        if(orderStationInsertList.size()!=0){
//            for (OrderStation list:orderStationInsertList  ) {
//                orderStationMapper.insertOrderStation(list);
//            }
            orderTechMapper.orderStationInsertList(orderStationInsertList);
        }
        if(orderDeteInsertList.size()!=0){
//            for (OrderDete list:orderDeteInsertList  ) {
//                orderDeteMapper.insertOrderDete(list);
//            }
            orderTechMapper.orderDeteInsertList(orderDeteInsertList);
        }
        if(orderBomInsertList.size()!=0){
//            for (OrderBom list:orderBomInsertList  ) {
//                orderBomMapper.insertOrderBom(list);
//            }
            orderTechMapper.orderBomInsertList(orderBomInsertList);
        }
        if(orderBadphenoInsertList.size()!=0){
//            for (OrderBadpheno list:orderBadphenoInsertList  ) {
//                orderBadphenoMapper.insertOrderBadpheno(list);
//            }
            orderTechMapper.orderBadphenoInsertList(orderBadphenoInsertList);
        }
        if(orderRepairFlowInsertList.size()!=0){
//            for (OrderRepairFlow list:orderRepairFlowInsertList  ) {
//                orderRepairFlowMapper.insertOrderRepairFlow(list);
//            }
            orderTechMapper.orderRepairFlowInsertList(orderRepairFlowInsertList);
        }
        if(orderRepairDeteInsertList.size()!=0){
//            for (OrderRepairDete list:orderRepairDeteInsertList  ) {
//                orderRepairDeteMapper.insertOrderRepairDete(list);
//            }
            orderTechMapper.orderRepairDeteInsertList(orderRepairDeteInsertList);
        }




        return  newOrderTechId;
        //复制流程完成
    }


}
